home *** CD-ROM | disk | FTP | other *** search
Wrap
package horst; import horst.parser.Tag; import java.awt.Color; import java.awt.Component; import java.awt.Container; import java.awt.Dimension; import java.awt.Font; import java.awt.FontMetrics; import java.awt.Frame; import java.awt.Graphics; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.Image; import java.awt.Insets; import java.awt.MediaTracker; import java.awt.Polygon; import java.awt.Rectangle; import java.awt.Shape; import java.awt.Toolkit; import java.net.MalformedURLException; import java.net.URL; import java.util.Hashtable; import java.util.StringTokenizer; import java.util.Vector; import javax.swing.AbstractButton; import javax.swing.Icon; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JCheckBox; import javax.swing.JComponent; import javax.swing.JRadioButton; import javax.swing.JTextArea; import javax.swing.JTextField; import javax.swing.border.EmptyBorder; import javax.swing.text.JTextComponent; import javax.swing.text.PlainDocument; public class Utilities { static boolean m_bDebugging; static Color _brightColor = stringToColor("#dcdcdc"); static Color _darkColor; static { _darkColor = _brightColor.darker().darker(); } public static final void addComponent(Container con, Component comp, int anchor, int fill, int gridheight, int gridwidth, int gridx, int gridy, Insets insets, int ipadx, int ipady, double weightx, double weighty) { GridBagLayout gridbag = (GridBagLayout)con.getLayout(); GridBagConstraints c = new GridBagConstraints(); c.anchor = anchor; c.fill = fill; c.gridheight = gridheight; c.gridwidth = gridwidth; c.gridx = gridx; c.gridy = gridy; c.insets = insets; c.ipadx = ipadx; c.ipady = ipady; c.weightx = weightx; c.weighty = weighty; gridbag.setConstraints(comp, c); con.add(comp); } public static final int[] clone(int[] ary) { int[] copy = new int[ary.length]; for(int i = 0; i < copy.length; ++i) { copy[i] = ary[i]; } return copy; } static final Component createFormComponent(HTMLDocument doc, Tag t) { Component c = null; if (t.getID() == 30) { String type = (String)t.getAttribute("type"); if (type == null) { c = new TextInput(); setFieldAttributes(t, (TextInput)c); return c; } if (!type.equalsIgnoreCase("submit") && !type.equalsIgnoreCase("reset") && !type.equalsIgnoreCase("button")) { if (type.equalsIgnoreCase("image")) { String src = (String)t.getAttribute("src"); JButton button; try { URL base = doc.getBaseURL(); URL srcURL = new URL(base, src); Icon icon = new ImageIcon(srcURL); button = new JButton(icon); ((JComponent)button).setBorder(new EmptyBorder(new Insets(0, 0, 0, 0))); ((AbstractButton)button).setMargin(new Insets(0, 0, 0, 0)); ((JComponent)button).setOpaque(false); ((AbstractButton)button).setBorderPainted(false); } catch (MalformedURLException var9) { button = new JButton(); } c = button; } else if (type.equalsIgnoreCase("checkbox")) { c = new JCheckBox(); if (t.isAttributeDefined("checked")) { ((JCheckBox)c).setSelected(true); } ((JCheckBox)c).setBorder(new EmptyBorder(new Insets(0, 0, 0, 0))); ((JCheckBox)c).setOpaque(false); } else if (type.equalsIgnoreCase("radio")) { c = new JRadioButton(); if (t.isAttributeDefined("checked")) { ((JRadioButton)c).setSelected(true); } ((JRadioButton)c).setBorder(new EmptyBorder(new Insets(0, 0, 0, 0))); ((JRadioButton)c).setOpaque(false); } else if (type.equalsIgnoreCase("text")) { c = new TextInput(); setFieldAttributes(t, (TextInput)c); } else if (type.equalsIgnoreCase("password")) { c = new TextInput(); setFieldAttributes(t, (TextInput)c); } } else { String value = (String)t.getAttribute("value"); if (value == null) { if (type.equalsIgnoreCase("submit")) { value = "submit"; } else { value = "reset"; } } JButton button = new JButton(value); Dimension prefSize = ((JComponent)button).getPreferredSize(); prefSize.height = getButtonHeight(); ((JComponent)button).setPreferredSize(prefSize); ((JComponent)button).setOpaque(true); c = button; } } else if (t.getID() == 32) { int columns = 10; int rows = 10; String colStr = (String)t.getAttribute("cols"); if (colStr != null) { Integer val = getInteger(colStr); if (val != null && val > 0) { columns = val; } } String rowStr = (String)t.getAttribute("rows"); if (rowStr != null) { Integer val = getInteger(rowStr); if (val != null && val > 0) { rows = val; } } PlainDocument d = new PlainDocument(); JTextArea ta = new JTextArea(d, "", rows, columns); ta.setLineWrap(true); c = ta; } else { c = new TextInput(); setFieldAttributes(t, (TextInput)c); } return c; } public static final Shape createShape(String name, String coordinates) { Shape s = null; StringTokenizer st = new StringTokenizer(coordinates, ", ", false); if (name != null && name.equalsIgnoreCase("rect") || name == null && st.countTokens() == 4) { Rectangle r = new Rectangle(); s = r; int nCount = 0; while(st.hasMoreTokens()) { Integer iVal = getInteger(st.nextToken()); if (iVal != null) { switch (nCount) { case 0: r.x = iVal; ++nCount; break; case 1: r.y = iVal; ++nCount; break; case 2: r.width = iVal - r.x; ++nCount; break; case 3: r.height = iVal - r.y; ++nCount; } } } } else { Vector xVector = new Vector(); Vector yVector = new Vector(); boolean bIsXVal = true; while(st.hasMoreTokens()) { Integer iVal = getInteger(st.nextToken()); if (iVal != null) { if (bIsXVal) { xVector.addElement(iVal); } else { yVector.addElement(iVal); } bIsXVal = !bIsXVal; } } int nPoints = Math.min(xVector.size(), yVector.size()); int[] xPoints = new int[nPoints]; int[] yPoints = new int[nPoints]; for(int i = 0; i < xPoints.length; ++i) { xPoints[i] = (Integer)xVector.elementAt(i); } for(int i = 0; i < yPoints.length; ++i) { yPoints[i] = (Integer)yVector.elementAt(i); } s = new Polygon(xPoints, yPoints, nPoints); } return s; } static final void debugOut(String msg) { if (m_bDebugging) { System.out.println(msg); } } static final void drawBorder(Graphics g, Rectangle r) { g.setColor(_brightColor); g.drawLine(r.x, r.y + r.height - 1, r.x + r.width - 1, r.y + r.height - 1); g.drawLine(r.x + r.width - 1, r.y, r.x + r.width - 1, r.y + r.height - 1); g.setColor(_darkColor); g.drawLine(r.x, r.y, r.x + r.width - 1, r.y); g.drawLine(r.x, r.y, r.x, r.y + r.height - 1); } public static final void drawDottedLine(Graphics g, int x1, int y1, int x2, int y2) { if (x1 == x2) { if (y1 > y2) { int temp = y2; y2 = y1; y1 = temp; } if ((x1 & 1 ^ y1 & 1) != 0) { ++y1; } while(y1 < y2) { g.drawLine(x1, y1, x1, y1); y1 += 2; } } else if (y1 != y2) { throw new IllegalArgumentException("Only horizonatl and Vertical Lines"); } else { if (y1 > y2) { y1 = y2; } if ((x1 & 1 ^ y1 & 1) != 0) { ++x1; } while(x1 < x2) { g.drawLine(x1, y1, x1, y1); x1 += 2; } } } public static final void drawDottedRectangle(Graphics g, int x, int y, int w, int h) { int x2 = x + w; int y2 = y + h; drawDottedLine(g, x, y, x2, y); drawDottedLine(g, x2, y, x2, y2); drawDottedLine(g, x, y2, x2, y2); drawDottedLine(g, x, y, x, y2); } public static final void drawFocusBorder(Graphics g, Rectangle r) { g.setColor(Color.orange); for(int i = 0; i < 2; ++i) { g.drawRect(r.x + i, r.y + i, r.width - 2 * i, r.height - 2 * i); } } static final Color getBrightBorderColor() { return _brightColor; } static final int getButtonHeight() { return 25; } static final Color getDarkBorderColor() { return _darkColor; } public static final Frame getFrame(Component c) { if (c instanceof Frame) { return (Frame)c; } else { Frame frame = null; Component parent; for(parent = c.getParent(); parent != null && !(parent instanceof Frame); parent = parent.getParent()) { } if (parent != null) { frame = (Frame)parent; } else { frame = new Frame(); } return frame; } } static final Integer getInteger(String val) { Integer i = null; try { i = new Integer(val); } catch (NumberFormatException var2) { } return i; } public static final String getInternetFormat(String str) { String s = ""; for(int i = 0; i < str.length(); ++i) { char c = str.charAt(i); if (c == '\\') { s = s + "/"; } else { s = s + c; } } return s; } static final URL getURL(String src) { URL u = null; src = removeNewlines(src); try { u = new URL(src); } catch (MalformedURLException var2) { } return u; } static final URL getURL(URL baseURL, String src) { URL u = null; src = removeNewlines(src); try { u = new URL(baseURL, src); } catch (MalformedURLException var3) { } return u; } static final Color hexToColor(String value) { if (value.startsWith("#")) { String digits = value.substring(1, Math.min(value.length(), 7)); String hstr = "0x" + digits; try { Color c = Color.decode(hstr); return c; } catch (NumberFormatException var4) { } } return null; } public static final boolean isBlank(char[] data) { int len = data.length; for(int i = 0; i < len; ++i) { if (!Character.isWhitespace(data[i])) { return false; } } return true; } static final boolean isBlankSpaces(String txt) { if (txt != null && txt.length() != 0) { for(int i = 0; i < txt.length(); ++i) { if (txt.charAt(i) != ' ' && txt.charAt(i) != '\n') { return false; } } return true; } else { return true; } } static final Image loadImage(Component c, String path) { Image i = Toolkit.getDefaultToolkit().getImage(path); if (i == null) { return i; } else { MediaTracker tracker = new MediaTracker(c); tracker.addImage(i, 0); try { tracker.waitForID(0); if (tracker.isErrorAny()) { return null; } } catch (InterruptedException var4) { } return i; } } static final Integer parseInteger(String s, char c) { Integer i = null; int index; if ((index = s.indexOf(c)) != -1) { try { s = s.substring(0, index).trim(); i = new Integer(s); } catch (NumberFormatException var4) { } } return i; } static String removeNewlines(String s) { StringBuffer buf = new StringBuffer(); for(int i = 0; i < s.length(); ++i) { if (s.charAt(i) != '\n') { buf.append(s.charAt(i)); } } return buf.toString(); } static final String removeTrailingBackslashes(String text) { while(text.endsWith("/")) { text = text.substring(0, text.length() - 1); } return text; } static final int setAlignmentProperty(boolean bVertical, int defaultAlignment, Object key, Hashtable hash) { String val = (String)hash.get(key); if (val != null) { if (bVertical) { if (val.toLowerCase().equals("top")) { return 0; } if (val.toLowerCase().equals("bottom")) { return 2; } } else { if (val.toLowerCase().equals("right")) { return 2; } if (val.toLowerCase().equals("left")) { return 0; } if (val.toLowerCase().equals("center") || val.toLowerCase().equals("middle")) { return 1; } } } return defaultAlignment; } static final boolean setBooleanProperty(boolean defaultVal, Object key, Hashtable hash) { String src = (String)hash.get(key); if (src != null) { if (src.equalsIgnoreCase("yes") || src.equalsIgnoreCase("true") || src.equalsIgnoreCase("1")) { return true; } if (src.equalsIgnoreCase("no") || src.equalsIgnoreCase("false") || src.equalsIgnoreCase("0")) { return false; } } return defaultVal; } static final Color setColorProperty(Color defaultColor, Object key, Hashtable hash) { Object val = hash.get(key); if (val == null) { return defaultColor; } else if (val instanceof Color) { return (Color)val; } else { String str = (String)val; Color c = null; if (str != null) { c = stringToColor(str); } return c != null ? c : defaultColor; } } private static final void setFieldAttributes(Tag t, TextInput field) { ((JTextField)field).setFont(new Font("Courier", 0, 12)); ((JTextComponent)field).setOpaque(true); int size = 20; String len = (String)t.getAttribute("maxlength"); if (len != null) { Integer iVal = getInteger(len); if (iVal != null && iVal > 0) { size = iVal; } } ((JTextField)field).setColumns(size); String sz = (String)t.getAttribute("size"); if (sz != null) { Integer iVal = getInteger(sz); if (iVal != null && iVal > 0) { int h = getButtonHeight(); FontMetrics fm = Toolkit.getDefaultToolkit().getFontMetrics(((Component)field).getFont()); int w = iVal * field.getColumnWidth(); field.setPreferredSize(new Dimension(w, h)); } } String value = (String)t.getAttribute("value"); if (value != null) { ((JTextComponent)field).setText(value); } } static final int setIntegerProperty(int defaultVal, Object key, Hashtable hash) { String val = (String)hash.get(key); if (val != null && val.length() > 0) { Integer iVal = getInteger(val); if (iVal != null && iVal >= 0) { return iVal; } } return defaultVal; } static final float setPercentageProperty(float defaultVal, Object key, Hashtable hash) { String src = (String)hash.get(key); if (src != null) { int idx = src.indexOf(37); if (idx != -1) { Integer val = getInteger(src.substring(0, idx)); if (val != null && val > 0 && val <= 100) { return (float)val / 100.0F; } } } return defaultVal; } static final URL setURLProperty(URL baseURL, Object key, Hashtable hash) { URL u = null; String src = (String)hash.get(key); if (src != null && src.length() > 0) { if (baseURL != null) { u = getURL(baseURL, src); } else { u = getURL(src); } } return u; } static final Color stringToColor(String str) { if (str == null | str.length() == 0) { return null; } else { Color color = null; if (str.charAt(0) == '#') { color = hexToColor(str); } else if (str.equals("orange")) { color = Color.orange; } else if (str.equals("magenta")) { color = Color.magenta; } else if (str.equals("darkmagenta")) { color = Color.magenta.darker(); } else if (str.equalsIgnoreCase("gold")) { color = Color.orange; } else if (str.equalsIgnoreCase("Black")) { color = hexToColor("#000000"); } else if (str.equalsIgnoreCase("Silver")) { color = hexToColor("#C0C0C0"); } else if (str.equalsIgnoreCase("Gray")) { color = hexToColor("#808080"); } else if (str.equalsIgnoreCase("White")) { color = hexToColor("#FFFFFF"); } else if (str.equalsIgnoreCase("Maroon")) { color = hexToColor("#800000"); } else if (str.equalsIgnoreCase("Red")) { color = hexToColor("#FF0000"); } else if (str.equalsIgnoreCase("Purple")) { color = hexToColor("#800080"); } else if (str.equalsIgnoreCase("Fuchsia")) { color = hexToColor("#FF00FF"); } else if (str.equalsIgnoreCase("Green")) { color = hexToColor("#008000"); } else if (str.equalsIgnoreCase("Lime")) { color = hexToColor("#00FF00"); } else if (str.equalsIgnoreCase("Olive")) { color = hexToColor("#808000"); } else if (str.equalsIgnoreCase("Yellow")) { color = hexToColor("#FFFF00"); } else if (str.equalsIgnoreCase("Navy")) { color = hexToColor("#000080"); } else if (str.equalsIgnoreCase("Blue")) { color = hexToColor("#0000FF"); } else if (str.equalsIgnoreCase("Teal")) { color = hexToColor("#008080"); } else if (str.equalsIgnoreCase("Aqua")) { color = hexToColor("#00FFFF"); } else { color = hexToColor("#" + str); } return color; } } }